package com.zw.admin.server.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.aliyuncs.iot.model.v20180120.CreateProductResponse;
import com.aliyuncs.iot.model.v20180120.QueryProductResponse;
import com.aliyuncs.iot.model.v20180120.UpdateProductResponse;
import com.zw.admin.server.model.Dict;
import com.zw.admin.server.service.ChanSerivce;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zw.admin.server.page.table.PageTableRequest;
import com.zw.admin.server.page.table.PageTableHandler;
import com.zw.admin.server.page.table.PageTableResponse;
import com.zw.admin.server.page.table.PageTableHandler.CountHandler;
import com.zw.admin.server.page.table.PageTableHandler.ListHandler;
import com.zw.admin.server.dao.SysChanDao;
import com.zw.admin.server.model.SysChan;

import io.swagger.annotations.ApiOperation;


@Api(tags = "产品管理")
@RestController
@RequestMapping("/sysChans")
public class SysChanController {

    @Autowired
    private SysChanDao sysChanDao;

    @Autowired
    private ChanSerivce chanSerivce;

    @PostMapping
    @ApiOperation(value = "保存")
    @RequiresPermissions("sys:chanpin:add")
    public Map save(@RequestBody SysChan sysChan) {
        Map map  = chanSerivce.saveProduct(sysChan);
        return map;
    }

    @GetMapping("/{productKey}")
    @ApiOperation(value = "根据id获取")
    @RequiresPermissions("sys:chanpin:query")
    public QueryProductResponse.Data get(@PathVariable String productKey) {
        QueryProductResponse.Data sysChan = chanSerivce.queryProduct(productKey);
        return sysChan;
    }

    @PutMapping
    @ApiOperation(value = "修改")
    @RequiresPermissions("sys:chanpin:add")
    public UpdateProductResponse update(@RequestBody SysChan sysChan) {
        UpdateProductResponse response = chanSerivce.updateProduct(sysChan);
//        sysChanDao.update(sysChan);
        return response;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    @RequiresPermissions("sys:chanpin:query")
    public PageTableResponse list(PageTableRequest request) {
        request.setCurrentPage(request.getOffset()/request.getLimit()+1);
        return new PageTableHandler(new CountHandler() {
            @Override
            public int count(PageTableRequest request) {
//                return sysChanDao.count(request.getParams());
                return  chanSerivce.QueryProductList( request.getCurrentPage(), request.getLimit(),null).getTotal();
            }
        }, new ListHandler() {

            @Override
            public List list(PageTableRequest request) {
//                return sysChanDao.list(request.getParams(), request.getOffset(), request.getLimit());
                return  chanSerivce.QueryProductList( request.getCurrentPage(), request.getLimit(),null).getList();
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    @RequiresPermissions("sys:chanpin:del")
    public void delete(@PathVariable String  id) {
        chanSerivce.deleteIotProduct(id);
//        sysChanDao.delete(id);
    }


    @GetMapping("/all")
    @ApiOperation(value = "全部的产品类型列表")
    public List<Dict> getAllProductType(){
        List<Dict> productTypeList = sysChanDao.getAllProductType();
        System.out.println("获取到的list:"+productTypeList);
        return productTypeList;
    }
}
